﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entities;
using Utility;
using BUS;
using DAL;

public partial class A_LetsDeal_dathang : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            if (!IsPostBack)
            {
                load_data();
                load_drop();
            }
        }
        else
        {
            Response.Redirect("~/A_LetsDeal/giohang.aspx");
        }
    }

    protected void load_data()
    {
        GIOHANG_DAL c = (GIOHANG_DAL)Session["Cart"];
        lit_tieude1.Text = "Thông tin khách hàng";
        lit_tieude2.Text = "Thông tin đơn hàng";
        Tbl_KhachHang_Entities kh = Tbl_KhachHang_BUS.GetByUsername(Session["username"].ToString());
        txt_hoten.Text = kh.HoTen;
        txt_dc.Text = kh.DiaChi;
        txt_dienthoai.Text = kh.SDT;
        lit_sosp.Text = c.ItemCollection.Count.ToString();
        float sl = 0;
        for (int i = 0; i < c.ItemCollection.Count; i++)
        {
            sl = c.ItemCollection[i].SOLUONG;
        }
        lit_sovc.Text = sl.ToString();
        lit_thanhtien.Text = "<font color='red'>" + String.Format("{0:0,0 đ}",c.Total()) + "</font>";
    }

    protected void load_drop()
    {
        Tbl_HTTT_Collection nvCollection = Tbl_HTTT_BUS.GetAll();
        ddl_httt.DataTextField = Tbl_HTTT_Columns.TenHTTT.ToString();
        ddl_httt.DataValueField = Tbl_HTTT_Columns.STT.ToString();
        ddl_httt.DataSource = nvCollection;
        ddl_httt.DataBind();
        ddl_httt.Items.Insert(0, new ListItem("Chọn hình thức thanh toán", "0"));

    }

    protected void bt_timkiem_Click(object sender, EventArgs e)
    {
        if (txt_timkiem.Text != "")
        {
            Response.Redirect("~/A_LetsDeal/Default.aspx?tensanpham=" + txt_timkiem.Text);
        }
    }
    protected void lbt_dathang_Click(object sender, EventArgs e)
    {
        GIOHANG_DAL c = (GIOHANG_DAL)Session["Cart"];
        //Thêm mới đơn hàng trước để lấy mã đơn hàng
        Tbl_DonHang_Entities dh = new Tbl_DonHang_Entities();
        dh.MaDH = DateTime.Now.Ticks.ToString();
        dh.NgayDat = DateTime.Now.ToString();
        dh.NguoiDat = Tbl_KhachHang_BUS.GetByUsername(Session["username"].ToString()).STT;
        dh.DiaChiNhanVoucher = txt_dc.Text;
        dh.DaGiao = false;
        dh.HTTT = ddl_httt.SelectedIndex;
        int madh = Tbl_DonHang_BUS.Insert(dh);

        //Tiếp tục thêm từng chi tiết hóa đơn
        for (int i = 0; i < c.ItemCollection.Count; i++)
        {
            Tbl_CTDH_Entities ctdh = new Tbl_CTDH_Entities();
            ctdh.DonHang = madh;
            ctdh.SanPham = c.ItemCollection[i].STT_sanpham;
            ctdh.SoLuong = c.ItemCollection[i].SOLUONG;
            Tbl_CTDH_BUS.Insert(ctdh);
            int slban = Tbl_SanPham_BUS.GetByID(c.ItemCollection[i].STT_sanpham).SLBan;
            int sldaban = Tbl_CTDH_BUS.CountByIDSP(c.ItemCollection[i].STT_sanpham);
            if (slban == sldaban)
            {
                Tbl_SanPham_Entities sp = new Tbl_SanPham_Entities();
                sp.STT = c.ItemCollection[i].STT_sanpham;
                sp.TinhTrang = "Cháy Hàng";
                sp.NgayKetThuc = DateTime.Now.ToString();
                Tbl_SanPham_BUS.Update1(sp);
            }
        }
        Session["Cart"] = null;
        Response.Redirect("~/A_LetsDeal/dathangthanhcong.aspx");
    }
}
